home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 January / EnigmA AMIGA RUN 33 (1999)(G.R. Edizioni)(IT)[!][issue 1999-01].iso / earcd / faq / computer-lang / ada / programming / part1.z / part1
Text File  |  1999-01-01  |  33KB  |  841 lines

  1. Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!spool.mu.edu!usenet.eel.ufl.edu!bofh.dot!usenet.cis.ufl.edu!purdue!lerc.nasa.gov!magnus.acs.ohio-state.edu!math.ohio-state.edu!howland.reston.ans.net!surfnet.nl!swsbe6.switch.ch!swidir.switch.ch!epflnews!dinews.epfl.ch!Magnus.Kempe
  2. From: Magnus.Kempe@di.epfl.ch (Magnus Kempe)
  3. Newsgroups: comp.lang.ada,comp.answers,news.answers
  4. Subject: Ada FAQ: Programming with Ada (part 1 of 4)
  5. Followup-To: poster
  6. Date: 30 May 1996 17:08:02 GMT
  7. Organization: None
  8. Lines: 816
  9. Sender: magnus@lglsun4.epfl.ch (Magnus Kempe)
  10. Approved: news-answers-request@MIT.EDU
  11. Distribution: world
  12. Message-ID: <4okkli$idt@disunms.epfl.ch>
  13. Reply-To: Magnus.Kempe@di.epfl.ch (Magnus Kempe)
  14. NNTP-Posting-Host: lglsun4.epfl.ch
  15. Mime-Version: 1.0
  16. Content-Type: text/plain; charset=iso-8859-1
  17. Content-Transfer-Encoding: 8bit
  18. Summary: Ada Programmer's Frequently Asked Questions (and answers),
  19.            part 1 of 4.
  20.          Please read before posting.
  21. Keywords: advanced language, artificial languages, computer software,
  22.           data processing, programming languages, Ada
  23. Xref: senator-bedfellow.mit.edu comp.lang.ada:45672 comp.answers:18995 news.answers:73079
  24.  
  25. Archive-name: computer-lang/Ada/programming/part1
  26. Comp-lang-ada-archive-name: programming/part1
  27. Posting-Frequency: monthly
  28. Last-modified: 22 May 1996
  29. Last-posted: 23 April 1996
  30.  
  31.                                Ada Programmer's
  32.                        Frequently Asked Questions (FAQ)
  33.  
  34.    IMPORTANT NOTE: No FAQ can substitute for real teaching and
  35.    documentation. There is an annotated list of Ada books in the
  36.    companion comp.lang.ada FAQ.
  37.  
  38.     Recent changes to this FAQ are listed in the first section after the table
  39.     of contents. This document is under explicit copyright.
  40.  
  41. This is part 1 of a 4-part posting; part 1 contains the table of contents.
  42. Part 2 begins with question 5.
  43. Part 3 begins with question 6.
  44. Part 4 begins with question 9.
  45. They should be the next postings in this thread.
  46.  
  47.  
  48. Introduction
  49.  
  50.    Ada is an advanced, modern programming language, designed and
  51.    standardized to support and strongly encourage widely recognized
  52.    software engineering principles: reliability, portability, modularity,
  53.    reusability, programming as a human activity, efficiency,
  54.    maintainability, information hiding, abstract data types, genericity,
  55.    concurrent programming, object-oriented programming, etc.
  56.  
  57.    All validated Ada compilers (i.e. a huge majority of the commercial
  58.    Ada compilers) have passed a controlled validation process using an
  59.    extensive validation suite. Ada is not a superset or extension of any
  60.    other language. Ada does not allow the dangerous practices or effects
  61.    of old languages, although it does provide standardized mechanisms to
  62.    interface with other languages such as Fortran, Cobol, and C.
  63.  
  64.    Ada is recognized as an excellent vehicle for education in programming
  65.    and software engineering, including for a first programming course.
  66.  
  67.    Ada is defined by an international standard (the language reference
  68.    manual, or LRM), which has been revised in 1995. Ada is taught and
  69.    used all around the world (not just in the USA). Ada is used in a very
  70.    wide range of applications: banking, medical devices,
  71.    telecommunications, air traffic control, airplanes, railroad
  72.    signalling, satellites, rockets, etc.
  73.  
  74.    The latest version of this FAQ is always accessible through WWW as
  75.    http://lglwww.epfl.ch/Ada/FAQ/programming.html#title
  76.  
  77. Maintenance
  78.  
  79.    This FAQ is maintained on an individual volunteer basis, by Magnus
  80.    Kempe (Magnus.Kempe@di.epfl.ch). [Note: This is done as a hobby, not
  81.    in my capacity as an employee at the Swiss Federal Institute of
  82.    Technology. --MK]
  83.  
  84.    The coding style used in most of the example Ada code is my own, and
  85.    you'll have to live with it (you may want to adopt it :-).
  86.  
  87.  
  88.      _________________________________________________________________
  89.  
  90.    Opinions (if any) expressed are those of the submitters and/or
  91.    maintainer.
  92.      _________________________________________________________________
  93.  
  94. Table of Contents:
  95.  
  96.      * 1: Recent changes to this FAQ
  97.  
  98.      * 2: Information about this document
  99.  
  100.      * 3: Elementary Questions
  101.           + 3.1: How do I make operations directly visible without
  102.             "use"ing a package?
  103.           + 3.2: How do I assign to an array of length 1?
  104.           + 3.3: How do I create a C-style nul-terminated string?
  105.           + 3.4: How can I create an array of strings of various length?
  106.           + 3.5: I know an exception is raised, but my program quits with
  107.             no warning. Why?
  108.           + 3.6: I have only one task in my program, but it doesn't seem
  109.             to run. Why?
  110.           + 3.7: How do I increase the stack size for a task?
  111.           + 3.8: What's the difference between a type conversion and a
  112.             qualifier?
  113.           + 3.9: How do I avoid the potential space in front of
  114.             Integer'Image?
  115.           + 3.10: Why is an exception raised when giving a default
  116.             discriminant?
  117.           + 3.11: When I want an Integer type, what's wrong with just
  118.             using the predefined type Integer or Long_Integer? Why would
  119.             I ever want to declare new Integer types?
  120.           + 3.12: Since I can always declare my own portable integer
  121.             types, why would I ever want to use the predefined type
  122.             Integer?
  123.           + 3.13: I am learning Ada. Can I experiment with a game
  124.             program?
  125.           + 3.14: How can I do a non-blocking, keystroke-at-a-time read
  126.             from the terminal?
  127.  
  128.  
  129.      * 4: Advantages of Ada
  130.           + 4.1: Why use Ada?
  131.           + 4.2: Ada seems large and complex, why is it this way?
  132.           + 4.3: Is there a contest with fame and money for good Ada
  133.             programmers?
  134.  
  135.  
  136.      * 5: Object-Oriented Programming with Ada
  137.           + 5.1: Why does Ada have "tagged types" instead of classes?
  138.           + 5.2: Variant records seem like a dead feature now. When
  139.             should I use them instead of tagged types?
  140.           + 5.3: What is meant by "interface inheritance" and how does
  141.             Ada support it?
  142.           + 5.4: How do you do multiple inheritance in Ada 9X?
  143.           + 5.5: Why are Controlled types so, well, strange?
  144.           + 5.6: What do "covariance" and "contravariance" mean, and does
  145.             Ada support either or both?
  146.           + 5.7: What is meant by upcasting/expanding and
  147.             downcasting/narrowing?
  148.           + 5.8: How does Ada do "narrowing"?
  149.           + 5.9: What is the difference between a class-wide access type
  150.             and a "general" class-wide access type?
  151.  
  152.  
  153.      * 6: Ada Numerics
  154.           + 6.1: Where can I find anonymous ftp sites for Ada math
  155.             packages? In particular where are the random number
  156.             generators?
  157.           + 6.2: How can I write portable code in Ada 83 using predefined
  158.             types like Float and Long_Float? Likewise, how can I write
  159.             portable code that uses Math functions like Sin and Log that
  160.             are defined for Float and Long_Float?
  161.           + 6.3: Is Ada any good at numerics, and where can I learn more
  162.             about it?
  163.           + 6.4: How do I get Real valued and Complex valued math
  164.             functions in Ada 95?
  165.           + 6.5: What libraries or public algorithms exist for Ada?
  166.  
  167.  
  168.      * 7: Efficiency of Ada Constructs
  169.           + 7.1: How much extra overhead do generics have?
  170.           + 7.2: How does Ada compare to other languages in efficiency of
  171.             code?
  172.  
  173.  
  174.      * 8: Advanced Programming Techniques with Ada
  175.           + 8.1: How can I redefine the assignment operation?
  176.           + 8.2: Does Ada have automatic constructors and destructors?
  177.           + 8.3: Should I stick to a one package, one type approach while
  178.             writing Ada software?
  179.           + 8.4: What is the "Beaujolais Effect"?
  180.           + 8.5: What about the "Ripple Effect"?
  181.           + 8.6: How to write an Ada program to compute when one has had
  182.             too much alcohol to legally drive?
  183.           + 8.7: Does Ada have macros?
  184.  
  185.  
  186.      * 9: Ada and Other Programming Languages
  187.           + 9.1: Where can I find programs that will translate from [some
  188.             language] to Ada?
  189.           + 9.2: How can I convert Ada 83 sources to Ada 9X?
  190.           + 9.3: I hear that Ada is slower than Fortran or C, is that
  191.             true?
  192.           + 9.4: Isn't Ada less "elegant" than Eiffel?
  193.           + 9.5: Are there any papers detailing the differences between
  194.             Ada and C++?
  195.           + 9.6: I keep hearing that Ada is a "strongly typed language",
  196.             but it seems different from what's meant in C++. Are they
  197.             different?
  198.           + 9.7: I'm told Ada does all sorts of static type checking, but
  199.             can't you get the same effect using a tool like "lint" with
  200.             C?
  201.           + 9.8: Does Ada have something like the Standard Template
  202.             Library (STL) in C++, or like the components one finds in
  203.             Smalltalk environments?
  204.           + 9.9: Where can I find the equivalent of "printf" in Ada?
  205.  
  206.  
  207.      * 10: Interfacing with Ada
  208.           + 10.1: I am writing software that used the Distributed
  209.             Interactive Simulation (DIS) interface, does an interface
  210.             exist in Ada?
  211.           + 10.2: Is there any support for Common Object Request Broker
  212.             Architecture (CORBA) for Ada 9X?
  213.  
  214.  
  215.      * 11: Finding Additional Information
  216.           + 11.1: Where can I find Ada books?
  217.           + 11.2: Are there other Ada-related FAQs?
  218.           + 11.3: What is the "HBAP WWW Server"?
  219.  
  220.  
  221.      * 12: Pretty-printing and Measuring Ada Source Code
  222.           + 12.1: Is there software that generates a pretty PostScript
  223.             file from Ada source code?
  224.           + 12.2: I use vgrind to do "pretty printing" of my source. Is
  225.             there a vgrind definition for Ada?
  226.           + 12.3: How about a source code reformatter?
  227.           + 12.4: How can I count source lines of code (SLOC)?
  228.           + 12.5: Can I measure other things?
  229.  
  230.  
  231.      * 13: Credits
  232.  
  233.      * 14: Copying this FAQ
  234.  
  235.  
  236.      _________________________________________________________________
  237.  
  238. 1: Recent changes to this FAQ
  239.  
  240.      * 960522: "aimap" located (12.3).
  241.      * 960320: comparing the efficiency of Ada and other languages (7.2)
  242.        (submitted by D. Wheeler)
  243.      * 960228: references for redefining the assignment operation (8.1)
  244.        (switched questions 8.1 and 8.2).
  245.      * 960130: another game written in Ada (3.13).
  246.      * 960123: ARA contest for good Ada (4.3).
  247.      * 950925: removed a couple of non-programming questions.
  248.      * 950828: the change logs now indicate the section/question number.
  249.      * 950819: non-blocking, keystroke-at-a-time reading (3.14).
  250.      * 950819: some URL updates: AdaIC files keep moving around.
  251.      * 950630: corrected answers on task switching (3.6) and conversion
  252.        vs. qualifier (3.8).
  253.      * 950517: "general" class-wide access type (5.9).
  254.      * 950413: variant records vs. tagged types (5.2), submitted by
  255.        K. Shillington.
  256.      * 950406: why Ada does not and should not have macros (8.7).
  257.      * 950320: more on DIS (10.1).
  258.      * 950315: Ada is good at numerics (6.3), by J. Parker.
  259.      * 950309: drinking and driving example (8.6).
  260.      * 950306: added counting SLOC (12.4) and measuring (12.5).
  261.      * 950222: fixed some typos.
  262.      * 950207: revised introduction.
  263.      * 950202: updated discussion of C++ STL and Smalltalk library (9.8).
  264.      * 950126: advantages of code sharing for generics (7.1); Pascal to
  265.        Ada tool (9.1).
  266.      * 950125: why define new integer types (3.11), and why use the
  267.        predefined Integer type (3.12), submitted by J. Parker.
  268.      * 950124: approved for posting in *.answers.
  269.      * 950116: converting Ada 83 code to Ada 9X (9.2).
  270.      * 950106: lengthy code sections extracted and put on FTP server.
  271.      * 950105: printf solution (9.9); update on exception traces and
  272.        vgrind.
  273.      * 950104: links from TOC to all questions.
  274.  
  275.  
  276.    What's important and missing:
  277.      * everything, life, and 42
  278.  
  279.  
  280.      _________________________________________________________________
  281.  
  282.  
  283. 2: Information about this document
  284.  
  285.    This file is posted monthly to comp.lang.ada, comp.answers, and
  286.    news.answers.
  287.  
  288.    This document has a home on the Home of the Brave Ada Programmers
  289.    (HBAP) WWW Server, in hypertext format, URL
  290.    http://lglwww.epfl.ch/Ada/FAQ/programming.html
  291.  
  292.    It is available --as posted in *.answers-- on rtfm.mit.edu, which
  293.    archives all FAQ files posted to *.answers; see
  294.    ftp://rtfm.mit.edu/pub/usenet-by-group/news.answers/computer-lang/Ada
  295.  
  296.    The text-only version is also available in directory
  297.    ftp://lglftp.epfl.ch/pub/Ada/FAQ
  298.  
  299.    Magnus Kempe maintains this document; it's a hobby, not a job.
  300.    Feedback (corrections, suggestions, ideas) about it is to be sent via
  301.    e-mail to Magnus.Kempe@di.epfl.ch
  302.    Thanks.
  303.  
  304.    In all cases, the most up-to-date version of the FAQ is the version
  305.    maintained on the HBAP WWW Server. Please excuse any formatting
  306.    inconsistencies in the posted version of this document, as it is
  307.    automatically generated from the on-line version.
  308.  
  309.      _________________________________________________________________
  310.  
  311.  
  312. 3: Elementary Questions
  313.  
  314.  
  315. 3.1: How do I make operations directly visible without "use"ing the package?
  316.  
  317.    In Ada 83, you can rename the operations in your scope.
  318.  
  319.      -- Say you have an integer type called Int in package Types
  320.      function "<" (Left, Right : Types.Int)
  321.        return Boolean
  322.        renames Types."<";
  323.      -- Make sure the profiles of the first and last "<" match!
  324.  
  325.  
  326.    For operators, Ada 95 introduces the "use type" clause:
  327.  
  328.      use type Types.Int; -- makes operators directly visible
  329.  
  330.  
  331. 3.2: How do I assign to an array of length 1?
  332.  
  333.    Because of ambiguity of parentheses, named notation must be used for
  334.    one-element aggregates (or, under a different angle: a positional
  335.    aggregate must have more than one component).
  336.  
  337.    See [RM9X 4.3.3(7)] as well as the syntax rule of
  338.    positional_array_aggregate in [RM9X 4.3.3]; historians see [RM83
  339.    4.3(4)].
  340.  
  341.      declare
  342.        Array_of_One : array (1..1) of Float;
  343.      begin
  344.        -- Array_of_One := (10.0);   -- Won't work, parsed as an expression
  345.                                     -- within parentheses
  346.  
  347.        Array_of_One := (1 => 10.0); -- No ambiguity here
  348.      end;
  349.  
  350.  
  351.    You can't write a one-element positional aggregate in Ada. Nor a
  352.    zero-element aggregate. The reason for this restriction is that it
  353.    would be difficult for compilers to determine whether:
  354.  
  355.      ( exp )
  356.  
  357.    is a parenthesized expression of some type, or an aggregate of an
  358.    array type. If Ada had used some other notation for aggregates (say,
  359.    "[...]"), then this problem would not exist.
  360.  
  361.    Apparently the original requirements for Ada forbade using certain
  362.    ASCII characters, like '[' and ']', because those characters were not
  363.    available on all hardware. Also, certain characters are used for
  364.    different purposes and glyphs in countries that need additional
  365.    letters not present in ASCII.
  366.  
  367.  
  368. 3.3: How do I create a C-style nul-terminated string?
  369.  
  370.    In a declaration block, append an ASCII.NUL to create a constant Ada
  371.    string.
  372.  
  373.      declare
  374.        Str_Nul : constant String := Str & ASCII.NUL;
  375.      begin
  376.        Call_Requiring_C_String (Str_Nul (Str_Nul'First)'Address);
  377.      end;
  378.  
  379. -- or --
  380.  
  381.      function Nul_Terminate (Str : String)
  382.        return String is
  383.        Str_Nul : constant String := Str & ASCII.NUL;
  384.      begin
  385.        return Str_Nul;
  386.      end Nul_Terminate;
  387.  
  388.  
  389. 3.4: How can I create an array of strings of various length?
  390.  
  391.    In Ada 83, you have to use string access types and "new" to get
  392.    "ragged" arrays:
  393.  
  394.      type String_Access is
  395.        access String;
  396.  
  397.      Strings : constant array (Positive range 1..3) of String_Access
  398.              := ( 1 => new String'("One"),
  399.                   2 => new String'("Two"),
  400.                   3 => new String'("Three")
  401.                 );
  402.  
  403.  
  404.    In Ada 95, the process is simplified by using aliased constants:
  405.  
  406.      type String_Access is
  407.        access constant String;
  408.  
  409.      One : aliased constant String := "One";
  410.      Two : aliased constant String := "Two";
  411.      Three : aliased constant String := "Three";
  412.  
  413.      Strings : constant array (Positive range <>) of String_Access
  414.              := ( 1 => One'Access,
  415.                   2 => Two'Access,
  416.                   3 => Three'Access
  417.                 );
  418.  
  419.  
  420. 3.5: I know an exception is raised, but my program quits with no warning. Why?
  421.  
  422.  
  423.    On some Ada compilers, you have to manually "with" Text_IO before
  424.    exception information is diplayed to the terminal.
  425.  
  426.    On other Ada compilers, you must set an environment variable flag in
  427.    order to cause the exception information trace to be displayed.
  428.  
  429.  
  430. 3.6: I have only one task in my program, but it doesn't seem to run. Why?
  431.  
  432.    In Ada, the main procedure is automatically designated as a task. This
  433.    task may be running forever, thus starving your other task(s), because
  434.    round-robin scheduling (time-slicing) is not required (pre-emptive
  435.    scheduling applies to tasks with different levels of priority).
  436.  
  437.    If the task in question is getting starved, it's a programmer problem,
  438.    not an Ada problem. The programmer has to use an Ada compiler that
  439.    supports pragma Time_Slice, or do the scheduling himself (by changing
  440.    the implementation of his Ada program to ensure that no task starves
  441.    another).
  442.  
  443.    One solution is to explicitly put the main task to sleep within a loop
  444.    construct in order to avoid starvation of the other task(s), as in:
  445.  
  446.      procedure Main is
  447.        task Test;
  448.        task body Test is
  449.        begin
  450.          loop
  451.            delay 1.0;
  452.            Text_IO.Put_Line ("Test");
  453.          end loop;
  454.        end Test;
  455.      begin
  456.        loop
  457.          delay 20.0;
  458.          Text_IO.Put_Line ("Sleeping then writing");
  459.        end loop;
  460.      end Main;
  461.  
  462.  
  463. 3.7: How do I increase the stack size for a task?
  464.  
  465.    Define the task as a "task type" and then use a pragma representation
  466.    clause.
  467.  
  468.      task type A_Task_Type;
  469.      for A_Task_Type'STORAGE_SIZE use 10_000;
  470.      -- 10K bytes allocated to instances of A_Task_Type
  471.      A_Task : A_Task_Type;
  472.  
  473.  
  474. 3.8: What's the difference between a type conversion and a qualifier?
  475.  
  476.    Use a qualifier (tick) to tell the compiler what type it can expect;
  477.    this is strictly a compile-time issue: a qualifier "hints" the type,
  478.    usually to remove an ambiguity. Use a conversion to tell the compiler
  479.    to convert an expression from one type to another (usually within one
  480.    derivation hierarchy); this operation may require a change of
  481.    representation at run-time (e.g. in case of a representation clause
  482.    applying exclusively to the source type).
  483.  
  484.      A : Integer := Integer'(1);  -- this is a qualifier: same as ":= 1;"
  485.      B : Integer := Integer (1);  -- this is a conversion
  486.  
  487.  
  488. 3.9: How do I avoid the potential space in front of Integer'Image?
  489.  
  490.    Use the function Trim from package Ada.Strings.Fixed (you can
  491.    actually trim strings in many other useful ways):
  492.  
  493.      function My_Image (I : Integer)
  494.        return String is
  495.      begin -- My_Image
  496.        return Ada.Strings.Fixed.Trim (Integer'Image (I), Ada.Strings.Left);
  497.      end My_Image;
  498.  
  499.      ... My_Image (12) = "12" ...
  500.  
  501.  
  502.    In Ada 83, code a function that accepts a string and strips the
  503.    leading blank:
  504.  
  505.      function Strip_Leading_Blank (Str : String)
  506.        return String is
  507.      begin -- Strip_Leading_Blank
  508.        if Str (Str'First) = ' ' then
  509.          return Str (1+Str'First .. Str'Last);
  510.        else
  511.          return Str;
  512.        end if;
  513.      end Strip_Leading_Blank;
  514.  
  515.      ...
  516.  
  517.      function My_Image (I : Integer)
  518.        return String is
  519.      begin -- My_Image
  520.        return Strip_Leading_Blank (Integer'Image (I));
  521.      end My_Image;
  522.  
  523.      ... My_Image (12) = "12" ...
  524.  
  525.  
  526. 3.10: Why is an exception raised when giving a default discriminant?
  527.  
  528.    Let's assume you would like to model varying-length strings:
  529.  
  530.      type V_String (Size : Natural := 0) is
  531.        record
  532.          S : String (1 .. Size);
  533.        end record;
  534.  
  535.  
  536.    (from Robert Dewar)
  537.  
  538.    When you give a default discriminant, then one method (I actually
  539.    think it is the preferred method) of implementation is to allocate the
  540.    maximum possible length. Since your discriminant is of type Natural,
  541.    this clearly won't work!
  542.  
  543.    GNAT may compile it, but it won't run it, and indeed I consider it a
  544.    GNAT bug (on the todo list) that no warning is issued at compile time
  545.    for this misuse.
  546.  
  547.    Some compilers, notably Alsys and RR, have at least partially "solved"
  548.    this problem by introducing hidden pointers, but this to me is an
  549.    undesirable implementation choice.
  550.  
  551.    First, it means there is hidden heap activity, which seems
  552.    undesirable. In a language where pointers are explicit, it is
  553.    generally a good idea if allocation is also explicit, and certainly
  554.    for real-time work, hidden anything is worrisome.
  555.  
  556.    Second, it is not easy to do uniformly. Alsys ends up introducing
  557.    arbitrary restrictions on the composition of such types (try making an
  558.    array of them), and RR introduces non-contiguous representations,
  559.    which are legal but troublesome.
  560.  
  561.    To "solve" the problem yourself, just declare a reasonable maximum
  562.    length, and use a subtype representing this length as the subtype of
  563.    the discriminant:
  564.  
  565.      Max_Length : constant := 200;
  566.  
  567.      subtype Index is
  568.        Natural range 0 .. Max_Length;
  569.  
  570.      type V_String (Size : Index := 0) is
  571.        record
  572.          S : String (1 .. Size);
  573.        end record;
  574.  
  575.  
  576. 3.11: When I want an Integer type, what's wrong with just using the predefined
  577. type Integer or Long_Integer? Why would I ever want to declare new Integer
  578. types?
  579.  
  580.    If you declare 2 distinct integer types, for example,
  581.  
  582.      type Data_Index        is range 1..100;
  583.      type Time_Series_Index is range 0..2**15-1;
  584.  
  585.  
  586.    then objects of type Data_Index can't be assigned (directly) to
  587.    variables of type Time_Series_Index, and vice-versa. Likewise,
  588.    variables of these 2 types can't be mixed in arithmetical expressions
  589.    (without explicit type conversions). This may seem like a source of
  590.    endless irritation, but on the contrary, good progammers use it to
  591.    improve the clarity of their code, to make it more robust, and more
  592.    portable. The first 2 examples discuss this. The third example
  593.    discusses the declaration of machine-portable 32-bit integers.
  594.    Declaring objects of type Integer can be highly non-portable, and of
  595.    course type Long_Integer may not exist on some compilers.
  596.  
  597.     Example 1.
  598.  
  599.    Suppose you declare arrays using the above indices:
  600.  
  601.      type Time_Series is array (Time_Series_Index) of Float;
  602.      type Y_Axis_Data is array (Data_Index)        of Float;
  603.  
  604.      Measurement : Time_Series;
  605.  
  606.  
  607.    Now if you mistakenly try to iterate over one array with the index of
  608.    the other, the compiler can catch the error at compile time:
  609.  
  610.      for I in Data_Index loop
  611.         Sum := Sum + Measurement(I);  -- compilation error
  612.      end loop;
  613.  
  614.  
  615.     Example 2.
  616.  
  617.    This is lifted from Tucker Taft's brief introduction to Ada 95 in the
  618.    contributed papers section of the Ada World Wide Web homepage. Here
  619.    Tucker uses the Ada 95 unsigned integers, called modular types, in the
  620.    implementation of a protected type, which defines a disk control unit.
  621.    Modular types are integer types with "and", "or" and "xor" defined, so
  622.    systems programmers are likely to use them as bit masks. Just as the
  623.    array indices of the 2 arrays defined above are never meant to be
  624.    mixed, the modular integer types used to implement the disk control
  625.    unit are never meant to be mixed. To make sure the compiler enforces
  626.    this, they are declared as distinct types:
  627.  
  628.      type Flags   is mod 2**4;  -- a 4-bit flags field
  629.      type Control is mod 2**4;  -- A 4-bit control field
  630.  
  631.      Status_Mask  : constant Flags := 2#1001#;   -- Set first and last bits.
  632.      Status_Ready : constant Flags := 2#1000#;   -- Status = Ready
  633.  
  634.      Start_Xfr    : constant Control := 2#0001#; -- Initiate xfr command
  635.  
  636.  
  637.    Now if someone attempts to apply a Flag variable where a Control
  638.    variable should be used (or vice-versa) the compiler will catch the
  639.    error. This is especially important when the code is maintained by
  640.    programmers who did not write it.
  641.  
  642.     Remarks on Examples 1 and 2.
  643.  
  644.    1. Notice that in both examples the programmer was able to state his
  645.    intentions rather forcefully in the code - intentions that otherwise
  646.    might have been expressed much less forcefully in comment statements.
  647.    Because of Ada's strong typing model, the compiler was able to catch
  648.    errors at compile-time when the programmer's intentions were violated.
  649.  
  650.  
  651.    2. Notice also that the Integer declarations in the 2 examples are
  652.    machine portable, unlike Integer and Long_Integer. A compiler will
  653.    typically map these integer types onto the most efficient base type
  654.    that is available on the target machine.
  655.  
  656.     Example 3.
  657.  
  658.    Although the examples given above are good ones, it is not necessarily
  659.    a common practice to define a large number of distinct integer types.
  660.    In many cases it is appropriate to use (say) a 32-bit integer (or a
  661.    small number of such types) and declare appropriate subtypes of it
  662.    (them). To declare a portable 32-bit integer (or more accurately, the
  663.    most efficient integer that is at least 32-bits):
  664.  
  665.    type Int_tmp is range -2**31+1 .. 2**31-1;
  666.    type Integer_32 is range Int_tmp'Base'First..Int_tmp'Base'Last;
  667.  
  668.  
  669.    A compiler may reject this declaration if no suitable base type is
  670.    available, but this is rare. What happens is this: in order to
  671.    implement Int_tmp, the compiler chooses as the base type of Int_tmp an
  672.    integer type that is available on the target machine. This base type
  673.    is usually the most efficient integer that accomodates the range of
  674.    Int_tmp, which in turn is usually the machine's 32-bit integer. (It
  675.    might even be a 64-bit integer on some machines, in which case
  676.    Integer_32'Size = 64, and Integer_32'Last = 2**63-1. Maybe we should
  677.    not call it Integer_32!)
  678.  
  679.  
  680. 3.12: Since I can always declare my own portable integer types, why would I
  681. ever want to use the predefined type Integer?
  682.  
  683.    The language itself provides some guidance here. The predefined type
  684.    Integer is used by Ada in the implementation of a number of convenient
  685.    services. The following examples describe some of these services.
  686.    Notice that in most of the following examples, it is unlikely that it
  687.    will ever matter whether or not the predefined type Integer is
  688.    16-bits, 32-bits, 48-bits, or 64-bits.
  689.  
  690.    a) The exponentiation of X (written X**N) is defined by the language
  691.    for any floating point or integer X, provided N is of type Integer. (N
  692.    should be non-negative for integer X though.)
  693.  
  694.    b) Ada's predefined String type (really just a packed unconstrained
  695.    array of characters) uses an index of subtype Positive (i.e. type
  696.    Integer).
  697.  
  698.    c) The array index in the following "short-hand" array declaration is
  699.    implicitly defined to be type Integer:
  700.  
  701.      A : array(10..40) of Float;
  702.  
  703.  
  704.    d) The loop parameter I in the following for loop is implicitly
  705.    declared type Integer:
  706.  
  707.      for I in 10..40 loop
  708.       ...
  709.      end loop;
  710.  
  711.  
  712.    This application of type Integer is the one most likely to get you
  713.    into portability trouble. If you write: "for I in 1..2**17 loop", then
  714.    you get a constraint error on compilers that make Integer 16-bits,
  715.    because 2**17 is out of range of any Ada 16-bit integer.
  716.  
  717.  
  718. 3.13: I am learning Ada. Can I experiment with a game program?
  719.  
  720.    Of course.
  721.  
  722.    The Public Ada library (FTP wuarchive.wustl.edu) has a portable Ada
  723.    Tetris program in the languages/ada/misc/games directory. It uses
  724.    tasking, keyboard input, and ANSI screen graphics. Have fun!
  725.  
  726.    There is also "program Small", a tiny text adventure program, that you
  727.    can expand; it is documented at URL
  728.    http://lglwww.epfl.ch/Ada/Tutorials/Lovelace/small.htm
  729.  
  730.  
  731. 3.14: How can I do a non-blocking, keystroke-at-a-time read from the terminal?
  732.  
  733.    Use the procedure Text_IO.Get_Immediate [RM95 A.10.7(11)].
  734.  
  735.    If you don't have an Ada 95 compiler but have a POSIX binding, there
  736.    is a package using POSIX services that provides non-blocking,
  737.    keystroke-at-a-time access to the terminal. It is available by FTP in
  738.    file ftp://lglftp.epfl.ch/pub/Ada/FAQ/inkey.ada
  739.  
  740.      _________________________________________________________________
  741.  
  742. 4: Advantages of Ada
  743.  
  744. 4.1: Why use Ada?
  745.  
  746.    Think of it like this: We're the kid on the street corner, licking
  747.    that tasty ice cream cone on a hot summer day; an impish grin
  748.    decorates our face as we consume our cool confection. Meanwhile, other
  749.    kids gather round, noticing our pleasure. It matters not a whit that
  750.    they've just had a drink, or had their fill with supper -- they now
  751.    want ice cream. We offer no lecture on how good the ice cream is, we
  752.    simply demonstrate that we are happy, and let their memories carry
  753.    them to the nearest ice cream truck.
  754.  
  755.    (Sorry, I got a little carried away --DW).
  756.  
  757.  
  758. 4.2: Ada seems large and complex, why is it this way?
  759.  
  760.    (Robert Dewar, lead designer of the GNU Ada compiler, responds):
  761.  
  762.    During the Ada 9X development process we have often had fierce
  763.    arguments over the need to simplify proposals, and I pointed out some
  764.    time ago that the idea of simplicity is heavily overloaded:
  765.      * simple to implement
  766.      * simple to describe informally
  767.      * simple to describe formally
  768.      * results in simple programs
  769.      * simple to understand and/or remember
  770.      * short to describe
  771.  
  772.  
  773.    None of these goals are quite the same, and often they severely
  774.    conflict.
  775.  
  776.    If you listen to programming language design types, especially from
  777.    universities, they often have very little experience in programming,
  778.    and especially little experience in writing large delivered,
  779.    maintained software. That doesn't mean they know nothing about
  780.    programming languages, but it does tend to mean that their view of
  781.    complexity is skewed, and in particularly concentrates on the
  782.    simplicity of the language itself, rather than on the simplicity of
  783.    resulting programs.
  784.  
  785.    A lot of the creative tension in the 9X design process arose from this
  786.    same fundamental dichotomy. The design team tended to have a high
  787.    tolerance for language complexity (partly because they were very good
  788.    at understanding language details), but had a lot of experience in
  789.    actual large scale programming, and so their idea of simplicity was
  790.    biased heavily to simplifying Ada programs. The opposite voice,
  791.    worried about the simplicity of the language itself, represented by a
  792.    section of the DR's and ISO group (who, being a larger more diverse
  793.    group tended to reflect a wider view), considered that the design team
  794.    had gone too far in this direction. If you want to get a feel for the
  795.    transitions, look at the early versions of the 9X ILS, particularly
  796.    version 1.0.
  797.  
  798.    In retrospect, I think we came up with what is at least very close the
  799.    optimal balance. Tuck can speak for himself here more clearly than I
  800.    can speak for him, but I would guess that he and the other members of
  801.    the team recognize that you have to be able to sell the resulting
  802.    design as an acceptably simple whole, and thus must step back from the
  803.    most extensive proposals, while on the other hand, the more
  804.    conservative KISS sentiments were convinced to accept more features
  805.    than they originally felt comfortable with because of convincing
  806.    programming examples and discussions of resulting programming
  807.    complexity. The third wing of opinion ("I don't care what you think,
  808.    but if we can't implement it, then it's not much use!") was also
  809.    effectively fed in from the user-implementor teams.
  810.  
  811.    Is the result too complex? Time will tell, but I think the balance is
  812.    a successful blend.
  813.  
  814.  
  815. 4.3: Is there a contest with fame and money for good Ada programmers?
  816.  
  817.    Yes, they should enter the Ada Lovelace Programming Contest sponsored
  818.    by the Ada Resource Association (ARA).
  819.  
  820.    The Ada contest seeks to recognize the most readable, original,
  821.    reusable, and clear working Ada programs. Like the Ada programming
  822.    language, the contest is named in honor of the first programmer in
  823.    history, Lady Ada Lovelace.
  824.  
  825.    Every three months, the ARA will pay US$ 750 to the best Ada code
  826.    segment submitted. Submissions must be received by the 15th (midnight)
  827.    of the "contest month" and the award will be announced at the end of
  828.    the second month. A submission is made by emailing the source code to
  829.  
  830.         ara-contest@ocsystems.com
  831.  
  832.    The first contest closed December 15th, 1995. (The next contest month
  833.    is March 1996.)
  834.  
  835.    The rules and guidelines of the contest are available from the Ada
  836.    Contest WWW Home at http://lglwww.epfl.ch/Ada/Contest/announce.html
  837.  
  838.    This contest is open to all. Sharpen your designs, code, comments, and
  839.    demos; show the world how good and unobfuscated your Ada code is, and
  840.    win the prize!
  841.